home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1706 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: cscsun3.larc.nasa.gov!hook
  2. From: hook@cscsun3.larc.nasa.gov (Ed Hook)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: realloc->free?
  5. Date: 16 Jan 1996 13:34:50 GMT
  6. Organization: CSC/NASA Langley Research Center
  7. Distribution: world
  8. Message-ID: <4dg9hq$fjn@reznor.larc.nasa.gov>
  9. References: <4daa2e$oh5@axe.netdoor.com> <4df2ud$706@oxy.rust.net>
  10. Reply-To: hook@cscsun3.larc.nasa.gov
  11. NNTP-Posting-Host: cscsun3.larc.nasa.gov
  12.  
  13. In article <4df2ud$706@oxy.rust.net>, ebennett@rust.net writes:
  14. |> esargent@netdoor.com (Eric Sargent) wrote:
  15. |> 
  16. |> 
  17. |> 
  18. |> >    Now let's say realloc had to move the data so a != b.  Does realloc
  19. |> >free the memory previously pointed to by a or should it be explicitly
  20. |> >freed if realloc returns a new location?  I checked the FAQ, but there
  21. |> >was nothing specific about realloc.  Thanks for any information.
  22. |> 
  23. |> realloc() will free the old block.  It is perfectly legal to say
  24. |> 
  25. |>   a = realloc(a, newsize);
  26. |>
  27.   Yes -- this _is_ perfectly legal.
  28.  
  29. |> No memory loss should occur from this.
  30. |>
  31.   Nope -- 'realloc()' can fail, a situation that it communicates by 
  32.   returning NULL. If this happens, you've leaked the memory to which
  33.   'a' pointed. The *safe* approach is somewhat more anal-retentive:
  34.  
  35.     tmp_ptr = realloc(a,newsize);
  36.     if ( tmp_ptr )
  37.         a = tmp_ptr;
  38.     else {
  39.         /* deal with the situation -- 'a' is still valid */
  40.     }
  41.  
  42. |> Earl
  43. |> 
  44. |> 
  45.  
  46. -- 
  47.  Ed Hook                              |       Coppula eam, se non posit
  48.  Computer Sciences Corporation        |         acceptera jocularum.
  49.  NASA Langley Research Center         | Me? Speak for my employer?...<*snort*>
  50.  Internet: hook@cscsun3.larc.nasa.gov |        ... Get a _clue_ !!! ...
  51.